require(['./config'], () => {
  require(['template', 'header', 'footer','cookie'], (template,header) => {
    class Cart {
      constructor() {
        this.init()
      }
      init () {
        let cartList = localStorage.getItem('cart')
        if (cartList) {
          this.cartList = JSON.parse(cartList)
          $('#shopping_car').html(template('srcShop', { cartList: this.cartList }))
          this.calcTotalMoney()
          //给所有单选绑事件
          this.checksChange()
          //全选
          this.allCheck()
          //增加商品数量
          this.addShop()
          //减少商品数量
          this.subShop()
          //删除商品
          this.delShop()
          //修改输入框的值
          this.updata()
          //下单结算
          this.pay()
        } else {
          $('#cart-empty').show()
          $('#shopping_car').hide()
        }
      }//计算总价
      calcTotalMoney () {
        var totalMoney = this.cartList.reduce((money, cart) => {
          if (cart.check) money += cart.price * cart.num
          return money
        }, 0)
        $('#payMoney').html(totalMoney.toFixed(2))
      }
      checksChange () {
        const _this = this;
        //给所有的单选按钮绑定事件
        $('#shopping_car  .main input:checkbox').on('change', function () {
          //获取当前商品的id
          let id = $(this).parents('ul').attr('data-id')
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) cart.check = $(this).prop('checked')
            return cart
          })
          //重新存localstorage
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          //全选按钮根据数据是否全选来渲染状态
          $('#all-check').prop('checked', _this.cartList.every(cart => {
            return cart.check
          }))
          //计算总价
          _this.calcTotalMoney()
        })
      }
      allCheck () {
        const _this = this;
        $('#all-check').prop('checked',_this.cartList.every(cart=>{
          return cart.check
        }))
        $('#shopping_car #all-check').on('change', function () {
          _this.cartList = _this.cartList.map(cart => {
            cart.check = $('#all-check').prop('checked')
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          var check = _this.cartList.every(cart => {
            return cart.check
          })
          if (check) {
            $('#shopping_car  .main input:checkbox').prop('checked', true)
          } else {
            $('#shopping_car  .main input:checkbox').prop('checked', false)
          }
          _this.calcTotalMoney()

        })
      }

      addShop () {
        const _this = this;
        $('#shopping_car #add').on('click', function () {
          let id = $(this).parents('ul').attr('data-id')
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              cart.num++;
            }
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          _this.init()
        })
      }

      subShop () {
        const _this = this;
        $('#shopping_car #sub').on('click', function () {
          let id = $(this).parents('ul').attr('data-id')
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              if (cart.num > 1) {
                cart.num--;
              }
            }
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          _this.init()
        })
      }
      updata(){
        const _this = this;
        $('#shopping_car .modify').on('keyup',function(){
          let id=$(this).parents('ul').attr('data-id')
          _this.cartList = _this.cartList.map(cart => {
            if (cart.id == id) {
              cart.num=parseInt($(this).val())
            }
            return cart
          })
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          _this.init()
        })
      }

      delShop () {
        const _this = this;
        // console.log(11)
        $(' #dele').on('click', function () {
          // console.log(12)
          let id = $(this).parents('ul').attr('data-id')
          if (confirm("确定要删除该商品吗？")) {
            _this.cartList = _this.cartList.filter(cart => {
              return cart.id != id
            })
          }
          localStorage.setItem('cart', JSON.stringify(_this.cartList))
          $(this).parents('ul').remove()
          let cart = localStorage.getItem('cart')
          if (cart === "[]") {
            localStorage.removeItem('cart')
          }
          _this.init()
          _this.calcTotalMoney()
          header.calcCartNum()
        })



      }

      pay(){
        $('#overbooking').on('click',function(){
          let info = $.cookie('userInfo')
          if(info){
            window.location.href = '/html/payment.html'
          }else{
            alert('请先登录，即将跳转到登录页面')
            window.location.href = '/html/login.html'
          }
        })
      }

    }
    new Cart()
  })
})